/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.sql.Connection;
import java.sql.Time;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import kitaplaner.database.Database;
import kitaplaner.database.Kita;
import kitaplaner.database.Mitarbeiter;
import kitaplaner.database.Periode;

/**
 *
 * @author kristina
 */
public class PeriodenApp {
    Kita kita = new Kita();
    Database db = new Database();
    MitarbeiterApp maApp;
    Connection conn;

    public PeriodenApp(Kita kita, Connection conn)
    {
        this.conn = conn;
        this.kita = kita;
        maApp = new MitarbeiterApp(kita, conn);
    }

    public Periode getPeriodeByID(int id)
    {
        Periode periode = null;
        ArrayList <Periode> alPer = kita.getPerioden();

        for (int i = 0; i < alPer.size(); i++)
        {
            if (alPer.get(i).getId() == id)
            {
                periode = alPer.get(i);
            }
        }

        return periode;
    }

    public Periode getPeriodeByGrpIdSTET(int maID, Time start, Time end)
    {
        Periode periode = null;
        ArrayList <Periode> alPer = kita.getPerioden();

        for (int i = 0; i < alPer.size(); i++)
        {
            if (alPer.get(i).getMaId() == maID
             && alPer.get(i).getStart() == start
             && alPer.get(i).getEnd() == end)
            {
                periode = alPer.get(i);
            }
        }

        return periode;
    }

    public Boolean insertIntoPeriode (int maId, Time start, Time end)
    {
        Boolean success = false;
        Periode periode = new Periode ();
        ArrayList <Periode> alPeriode = kita.getPerioden();

        periode.setMaId(maId);
        periode.setEnd(end);
        periode.setStart(start);

        alPeriode.add(periode);
        kita.setPerioden(alPeriode);

        return success;
    }

    public Boolean insertPeriodeEntry (int maid, Time start, Time end)
    {
        Boolean success = false;
        Periode s = new Periode ();
        
        s.setMaId(maid);
        s.setEnd(end);
        s.setStart(start);

        conn = db.checkConnection(conn);
        success = db.insertPeriod(conn, s);
        conn = db.checkConnection(conn);
        kita.setPerioden(db.getPerioden(conn));

        return success;
    }

    public Boolean deletePeriodeEntry (int id)
    {
        Boolean success = false;
        ArrayList <Periode> aS = kita.getPerioden();

        for (int i = 0; i < aS.size(); i ++)
        {
            if (aS.get(i).getId() == id)
            {
                aS.remove(i);
            }
        }

        conn = db.checkConnection(conn);
        success = db.deletePeriode(conn, id);

        kita.setPerioden(aS);

        return success;
    }

    public Object [] createCompoboxText()
    {
        Object [] comboItems;

        ArrayList <Periode> p = kita.getPerioden();
        ArrayList pNames = new ArrayList();

        for (int i = 0; i < p.size(); i++)
        {
            pNames.add(p.get(i).getId());
        }
        comboItems = pNames.toArray();

        return comboItems;
    }

    // Wieso wird das zur Initialisierung des gesamten Programms benötigt?
    public DefaultTableModel createPeriodOverview()
    {
        String[] columnNames = {"ID", "Mitarbeiter Name", "Startzeit",
                                "Endzeit"};
        ArrayList <Periode> per = kita.getPerioden();
        DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);
        int grpId = 0;

        //insert Columns
        for (int i = 0 ; i < columnNames.length; i++)
        {
            model.addColumn(columnNames[i]);
        }

        for (int i = 0; i < per.size(); i++)
        {
            Mitarbeiter ma = maApp.getMaById(per.get(i).getMaId());
            String maName = ma.getVorname() + " " + ma.getNachname();
            Object []obj = {per.get(i).getId(), maName, per.get(i).getStart(),
                            per.get(i).getEnd()};
            model.insertRow (model.getRowCount(), obj);
        }

        return model;
    }
}
